package org.abratuhi.kvngroupware.brainstorm.mptua;

import java.util.ArrayList;

import org.abratuhi.kvngroupware.brainstorm.manager.BrainstormManager;
import org.abratuhi.kvngroupware.brainstorm.model.Brainstorm;
import org.abratuhi.kvngroupware.network.AbstractRequestMessageProceeder;
import org.abratuhi.kvngroupware.network.Client;
import org.abratuhi.kvngroupware.network.Message;
import org.abratuhi.kvngroupware.network.MessageSender;
import org.abratuhi.kvngroupware.server.db.ServerDB;
import org.abratuhi.kvngroupware.user.manager.UserManager;
import org.abratuhi.kvngroupware.user.model.User;
import org.jdom.Element;

public class BrainstormRequestMessageProceeder extends AbstractRequestMessageProceeder{

	MessageSender sender;
	UserManager usermanager;
	BrainstormManager brainstormmanager;
	ServerDB db;

	public BrainstormRequestMessageProceeder(MessageSender sender, UserManager usermanager, BrainstormManager brainstormmanager, ServerDB db){
		this.sender = sender;
		this.usermanager = usermanager;
		this.brainstormmanager = brainstormmanager;
		this.db = db;
	}

	@Override
	public void proceedMessage(Client client, Message message) {
		Element request = message.getRoot().getChild("request");
		String type = request.getAttributeValue("type");
		String action = request.getAttributeValue("action");
		String brainstormId = request.getAttributeValue("brainstormId");
		String userId = request.getAttributeValue("userId");
		String teamId = request.getAttributeValue("teamId");
		String brainstormTheme = request.getAttributeValue("brainstormTheme");
		String tekst = request.getText();

		String[] attrNames = new String[]{"type", "action", "brainstormId",  "userId", "teamId", "theme", "status"};
		String[] attrValues= new String[]{type, action, brainstormId, userId, teamId, brainstormTheme, "ok"};
		String text = tekst;
		Element[] content = new Element[0];
		Message responseMessage;

		if(type.equals("brainstorm")){
			if(action.equals("create")){
				Brainstorm brainstorm = brainstormmanager.get(brainstormId);
				if(brainstorm == null){
					brainstorm = new Brainstorm(brainstormId, teamId, brainstormTheme);
					brainstormmanager.put(brainstorm.getId(), brainstorm);
				}
				else{
					attrValues[6] = "brainstormId_found";
				}
			}
			else if(action.equals("get")){
				ArrayList<Element> eBrainstorms = new ArrayList<Element>();
				for(int i=0; i<brainstormmanager.size(); i++){
					Element eBrainstorm = brainstormmanager.get(brainstormmanager.getkeys()[i]).toElement();
					eBrainstorms.add(eBrainstorm);
				}
				content = eBrainstorms.toArray(new Element[eBrainstorms.size()]);
			}
			else if(action.equals("delete")){
				Brainstorm brainstorm = brainstormmanager.get(brainstormId);
				if(brainstorm != null){
					brainstormmanager.remove(brainstorm.getId());
				}
				else{
					attrValues[6] = "brainstormId_notfound";
				}
			}
			else if(action.equals("join")){
				User user = usermanager.get(userId);
				Brainstorm brainstorm = brainstormmanager.get(brainstormId);
				if(brainstorm != null && user != null && !brainstorm.containsMember(user)){
					brainstorm.addMember(user, "", "idle");
				}
				else if(brainstorm == null){
					attrValues[6] = "brainstormId_notfound";
				}
				else if(user == null){
					attrValues[6] = "userId_notfound";
				}
				else if(brainstorm.containsMember(user)){
					attrValues[6] = "userId_found";
				}
			}
			else if(action.equals("leave")){
				User user = usermanager.get(userId);
				Brainstorm brainstorm = brainstormmanager.get(brainstormId);

				if(brainstorm != null && user != null){
					brainstorm.removeMember(userId);
				}
				else if(brainstorm == null){
					attrValues[6] = "brainstormId_notfound";
				}
				else if(user == null){
					attrValues[6] = "userId_notfound";
				}
				// TODO redo?
				if(brainstorm != null && user != null && brainstorm.getMembers().size() == 0){
					db.addBrainstorm(brainstorm.toBackupElement());
					brainstormmanager.remove(brainstormId);

					String[] attrNames2 = new String[]{"type", "action", "brainstormId",  "userId", "teamId", "theme", "status"};
					String[] attrValues2= new String[]{type, "delete", brainstormId, userId, teamId, brainstormTheme, "ok"};
					String text2 = new String();
					Element[] content2 = new Element[0];
					Message responseMessage2 = sender.createMessage(attrNames2, attrValues2, text2, content2);
					sender.sendMessage(responseMessage2);
				}
			}
			else if(action.equals("commit")){
				User user = usermanager.get(userId);
				Brainstorm brainstorm = brainstormmanager.get(brainstormId);
				if(brainstorm != null && user != null){
					brainstorm.commitSheet(userId, tekst);
				}
				else if(brainstorm == null){
					attrValues[6] = "brainstormId_notfound";
				}
				else if(user == null){
					attrValues[6] = "userId_notfound";
				}
			}
			else if(action.equals("switch")){
				User user = usermanager.get(userId);
				Brainstorm brainstorm = brainstormmanager.get(brainstormId);
				if(brainstorm != null && user != null){
					brainstorm.switchSheets();
				}
				else if(brainstorm == null){
					attrValues[6] = "brainstormId_notfound";
				}
				else if(user == null){
					attrValues[6] = "userId_notfound";
				}
			}
			else if(action.equals("store")){
				Brainstorm brainstorm = brainstormmanager.get(brainstormId);
				db.addBrainstorm(brainstorm.toBackupElement());
			}

			responseMessage = sender.createMessage(attrNames, attrValues, text, content);
			sender.sendMessage(responseMessage);
		}
	}

}
